
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>cursor类 &#8212; PyKBase 2.0.0 文档</title>
    <link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/language_data.js"></script>
    <script type="text/javascript" src="_static/translations.js"></script>
    <link rel="index" title="索引" href="genindex.html" />
    <link rel="search" title="搜索" href="search.html" />
    <link rel="next" title="TPIClient类" href="TPIClient.html" />
    <link rel="prev" title="connection类" href="connection.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <div class="section" id="cursor">
<h1>cursor类<a class="headerlink" href="#cursor" title="永久链接至标题">¶</a></h1>
<dl class="class">
<dt>
<em class="property">class </em><code class="sig-name descname">cursor</code></dt>
<dd><p>当与数据库建立连接以后,就可以与数据库进行交互.一个游标允许用户执行数据库命令和获取查询结果.一个Python DB-API游标对象总是扮演游标的角色,无论数据库是否真正支持游标,因此,数据库接口必须实现游标对象.只有这样,才能保证无论使用何种后端数据库你的代码都不要做任何改变.</p>
<p>创建游标对象之后,就可以执行查询或其他命令(或者多个查询和多个命令),也可以从结果集中取出一条或多条记录.</p>
<dl class="method">
<dt id="cursor.close">
<code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.close" title="永久链接至目标">¶</a></dt>
<dd><p>关闭游标对象</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>None</strong> – 无.</p>
</dd>
<dt class="field-even">返回</dt>
<dd class="field-even"><p>无.</p>
</dd>
</dl>
<p>例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">KBase</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">KBase</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span><span class="c1">#关闭游标对象</span>
<span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.execute">
<code class="sig-name descname">execute</code><span class="sig-paren">(</span><em class="sig-param">ksql</em><span class="optional">[</span>, <em class="sig-param">args</em>, <em class="sig-param">bunicode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.execute" title="永久链接至目标">¶</a></dt>
<dd><p>执行一个数据库查询或命令</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>ksql</strong> – ksql格式串.</p></li>
<li><p><strong>args</strong> – 格式串参数(可选).</p></li>
<li><p><strong>bunicode</strong> – 字段是否全部为unicode编码(可选).</p></li>
</ul>
</dd>
<dt class="field-even">返回</dt>
<dd class="field-even"><p>PyObject *</p>
</dd>
</dl>
<p>例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">KBase</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">KBase</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="c1">#整体</span>
<span class="n">ret0</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(&#39;郑一&#39;,&#39;1000&#39;,&#39;aa&#39;)&quot;</span><span class="p">)</span>
<span class="c1">#数字位置风格</span>
<span class="n">ret1</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(:1, :2, :3)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s2">&quot;王一&quot;</span><span class="p">,</span><span class="s2">&quot;1001&quot;</span><span class="p">,</span><span class="s2">&quot;aa&quot;</span><span class="p">))</span>
<span class="c1">#命名参数风格</span>
<span class="n">ret2</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(:姓名, :学号, :NAME)&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;姓名&#39;</span><span class="p">:</span><span class="s1">&#39;王二&#39;</span><span class="p">,</span> <span class="s1">&#39;学号&#39;</span><span class="p">:</span><span class="s1">&#39;1002&#39;</span><span class="p">,</span> <span class="s1">&#39;NAME&#39;</span><span class="p">:</span><span class="s1">&#39;bb&#39;</span><span class="p">})</span>
<span class="c1">#字典格式转换</span>
<span class="n">ret3</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(</span><span class="si">%(姓名)s</span><span class="s2">, </span><span class="si">%(学号)s</span><span class="s2">, </span><span class="si">%(NAME)s</span><span class="s2">)&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;姓名&#39;</span><span class="p">:</span><span class="s1">&#39;王三&#39;</span><span class="p">,</span> <span class="s1">&#39;学号&#39;</span><span class="p">:</span><span class="s1">&#39;1003&#39;</span><span class="p">,</span> <span class="s1">&#39;NAME&#39;</span><span class="p">:</span><span class="s1">&#39;cc&#39;</span><span class="p">})</span>
<span class="c1">#问号风格</span>
<span class="n">ret4</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(?, ?, ?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s2">&quot;王四&quot;</span><span class="p">,</span><span class="s2">&quot;1004&quot;</span><span class="p">,</span><span class="s2">&quot;dd&quot;</span><span class="p">))</span>
<span class="c1">#标准 ANSI C格式转换</span>
<span class="n">ret5</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(</span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s2">&quot;王五&quot;</span><span class="p">,</span><span class="s2">&quot;1005&quot;</span><span class="p">,</span><span class="s2">&quot;ee&quot;</span><span class="p">))</span>

<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.executemany">
<code class="sig-name descname">executemany</code><span class="sig-paren">(</span><em class="sig-param">ksql</em><span class="optional">[</span>, <em class="sig-param">args</em>, <em class="sig-param">bunicode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.executemany" title="永久链接至目标">¶</a></dt>
<dd><p>类似execute()和map()的结合，为给定的每一个参数准备并执行一个数据库查询/命令</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>ksql</strong> – ksql格式串.</p></li>
<li><p><strong>args</strong> – 格式串参数(可选).</p></li>
<li><p><strong>bunicode</strong> – 字段是否全部为unicode编码(可选).</p></li>
</ul>
</dd>
<dt class="field-even">返回</dt>
<dd class="field-even"><p>PyObject *</p>
</dd>
</dl>
<p>例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">KBase</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">KBase</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="c1">#字典格式转换</span>
<span class="n">sql1</span> <span class="o">=</span> <span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(</span><span class="si">%(姓名)s</span><span class="s2">, </span><span class="si">%(学号)s</span><span class="s2">, </span><span class="si">%(NAME)s</span><span class="s2">)&quot;</span>
<span class="n">args1</span> <span class="o">=</span> <span class="p">({</span><span class="s1">&#39;姓名&#39;</span><span class="p">:</span><span class="s1">&#39;王六&#39;</span><span class="p">,</span> <span class="s1">&#39;学号&#39;</span><span class="p">:</span><span class="s1">&#39;1006&#39;</span><span class="p">,</span> <span class="s1">&#39;NAME&#39;</span><span class="p">:</span><span class="s1">&#39;ff&#39;</span><span class="p">},</span>
        <span class="p">{</span><span class="s1">&#39;姓名&#39;</span><span class="p">:</span><span class="s1">&#39;王七&#39;</span><span class="p">,</span> <span class="s1">&#39;学号&#39;</span><span class="p">:</span><span class="s1">&#39;1007&#39;</span><span class="p">,</span> <span class="s1">&#39;NAME&#39;</span><span class="p">:</span><span class="s1">&#39;gg&#39;</span><span class="p">},</span>
        <span class="p">{</span><span class="s1">&#39;姓名&#39;</span><span class="p">:</span><span class="s1">&#39;王八&#39;</span><span class="p">,</span> <span class="s1">&#39;学号&#39;</span><span class="p">:</span><span class="s1">&#39;1008&#39;</span><span class="p">,</span> <span class="s1">&#39;NAME&#39;</span><span class="p">:</span><span class="s1">&#39;hh&#39;</span><span class="p">})</span>
<span class="n">ret6</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="n">sql1</span><span class="p">,</span> <span class="n">args1</span><span class="p">)</span>
<span class="c1">#标准 ANSI C格式转换</span>
<span class="n">sql2</span> <span class="o">=</span> <span class="s2">&quot;insert into TABLE1(姓名,学号,NAME) values(</span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">)&quot;</span>
<span class="n">args2</span> <span class="o">=</span> <span class="p">[(</span><span class="s2">&quot;王九&quot;</span><span class="p">,</span><span class="s2">&quot;1009&quot;</span><span class="p">,</span><span class="s2">&quot;ii&quot;</span><span class="p">),(</span><span class="s2">&quot;王十&quot;</span><span class="p">,</span><span class="s2">&quot;1010&quot;</span><span class="p">,</span><span class="s2">&quot;jj&quot;</span><span class="p">)]</span>
<span class="n">ret7</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="n">sql2</span><span class="p">,</span> <span class="n">args2</span><span class="p">)</span>

<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.fetchone">
<code class="sig-name descname">fetchone</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.fetchone" title="永久链接至目标">¶</a></dt>
<dd><p>取当前结果集的一行(从当前开始)</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>None</strong> – 无.</p>
</dd>
<dt class="field-even">返回</dt>
<dd class="field-even"><p>PyObject *</p>
</dd>
</dl>
<p>例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">KBase</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">KBase</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM </span><span class="si">%s</span><span class="s2"> WHERE&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;TABLE1&#39;</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span><span class="c1">#(&#39;王四&#39;, 1004, &#39;dd4&#39;)</span>

<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.fetchmany">
<code class="sig-name descname">fetchmany</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">size = 1</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.fetchmany" title="永久链接至目标">¶</a></dt>
<dd><p>取当前结果集的多行(从当前开始)</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>size</strong> – 所取记录条数(可选).</p>
</dd>
<dt class="field-even">返回</dt>
<dd class="field-even"><p>PyObject *</p>
</dd>
</dl>
<p>例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">KBase</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">KBase</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM </span><span class="si">%s</span><span class="s2"> WHERE&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;TABLE1&#39;</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchmany</span><span class="p">(</span><span class="n">size</span> <span class="o">=</span> <span class="mi">3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span><span class="c1">#[(&#39;王四&#39;, 1004, &#39;dd4&#39;), (&#39;王五&#39;, 1005, &#39;ee5&#39;), (&#39;王六&#39;, 1006, &#39;ff6&#39;)]</span>

<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="cursor.fetchall">
<code class="sig-name descname">fetchall</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cursor.fetchall" title="永久链接至目标">¶</a></dt>
<dd><p>取当前结果集剩下的所有行(从当前开始)</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>None</strong> – 无.</p>
</dd>
<dt class="field-even">返回</dt>
<dd class="field-even"><p>PyObject *</p>
</dd>
</dl>
<p>例:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">KBase</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">KBase</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM </span><span class="si">%s</span><span class="s2"> WHERE&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;TABLE1&#39;</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span><span class="c1">#[(&#39;王四&#39;, 1004, &#39;dd4&#39;), (&#39;王五&#39;, 1005, &#39;ee5&#39;), (&#39;王六&#39;, 1006, &#39;ff6&#39;), (&#39;王七&#39;, 1007, &#39;gg7&#39;), (&#39;王八&#39;, 1008, &#39;hh8&#39;), (&#39;王九&#39;, 1009, &#39;ii9&#39;), (&#39;王十&#39;, 1010, &#39;jj10&#39;), (&#39;张三&#39;, 1012, &#39;kk12&#39;), (&#39;张四&#39;, 1101, &#39;aa1&#39;), (&#39;张五&#39;, 1102, &#39;aa2&#39;)]</span>

<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>


          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">PyKBase</a></h1>








<h3>导航</h3>
<p class="caption"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="kbasemodule.html">KBase模块</a></li>
<li class="toctree-l1"><a class="reference internal" href="connection.html">connection类</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">cursor类</a></li>
<li class="toctree-l1"><a class="reference internal" href="TPIClient.html">TPIClient类</a></li>
<li class="toctree-l1"><a class="reference internal" href="TPIClient_Struct.html">TPIClient_Struct内容</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="connection.html" title="上一章">connection类</a></li>
      <li>Next: <a href="TPIClient.html" title="下一章">TPIClient类</a></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="转向" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, ZZJ.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 2.2.2</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
      |
      <a href="_sources/cursor.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>